name: "Build and populate cache"
on:
  pull_request:
  push:
    branches:
      - master
  schedule:
    - cron:  '51 2 * * *'
jobs:
  tests:
    strategy:
      matrix:
        nurRepo:
          - sikmir
        cachixName:
          - sikmir
        nixPath:
          - nixpkgs=https://github.com/NixOS/nixpkgs/archive/refs/heads/nixpkgs-unstable.tar.gz
        os:
          - ubuntu-latest
          - macos-latest
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    - name: Install nix
      uses: cachix/install-nix-action@v30
      with:
        nix_path: "${{ matrix.nixPath }}"
        extra_nix_config: |
          experimental-features = nix-command flakes
          access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
    - name: Show nixpkgs version
      run: nix-instantiate --eval -E '(import <nixpkgs> {}).lib.version'
    - name: Setup cachix
      uses: cachix/cachix-action@v15
      with:
        name: ${{ matrix.cachixName }}
        authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
    - name: Check evaluation
      run: |
        nix-env -f . -qa \* --meta --xml \
          --allowed-uris https://static.rust-lang.org \
          --option restrict-eval true \
          --option allow-import-from-derivation true \
          --drv-path --show-trace \
          -I nixpkgs=$(nix-instantiate --find-file nixpkgs) \
          -I $PWD
    - name: Build nix packages
      run: nix shell -f '<nixpkgs>' nix-build-uncached -c nix-build-uncached ci.nix -A cacheOutputs
      env:
        NIXPKGS_ALLOW_INSECURE: 1
    - name: Trigger NUR update
      run: curl -XPOST "https://nur-update.nix-community.org/update?repo=${{ matrix.nurRepo }}"
